package com.cn.crmsystem.controller.systemController;

import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.Role;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.systemService.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;

@RestController
@Api(value = "角色管理", tags = "角色增删查改数据接口API")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/selectRole", method = RequestMethod.POST)
    @ApiOperation(value = "查询(可筛选)角色接口", notes = "管理员查询角色接口", httpMethod = "POST", response = ResultJson.class)
    public ResultJson selectRole(@RequestBody @ApiParam(name = "role", value = "角色名称",required = false) Role role){
        System.out.println("----查询角色----");
        System.out.println(role);
        return roleService.selectRole(role);
    }

    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    @ApiOperation(value = "新增角色接口", notes = "管理员新增角色接口", httpMethod = "POST", response = ResultJson.class)
    public ResultJson addRole(@RequestBody @ApiParam(name = "role", value = "角色类类型对象数据",required = false) Role role){
        System.out.println("----新增角色----");
        Login login = (Login)SecurityUtils.getSubject().getPrincipal();
        System.out.println("-=-=-=-=-==-=-=="+login);
//        Login login = new Login();
//        login.setUser_id(1);
        System.out.println(login.getUser_id());
        return roleService.addRole(role,login);
    }

    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    @ApiOperation(value = "删除角色接口", notes = "管理员根据id删除角色", httpMethod = "POST", response = ResultJson.class)
    public ResultJson deleteRole(@RequestParam @ApiParam(name = "roleId", value = "角色id",required = false) Integer roleId){
        System.out.println("----删除角色----");
        return roleService.deleteRole(roleId);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteManyRole",method = RequestMethod.POST)
    @ApiOperation(value ="批量删除角色数据接口",notes = "传入要删除的数据id",httpMethod = "post",response = ResultJson.class)
    @ApiImplicitParam(name = "ids",value = "角色id",paramType = "query",dataType = "int",required = false)
    public ResultJson deleteManyRole(Integer[] ids) {
        System.out.println(ids.length);
        System.out.println(ids);
        return roleService.deleteManyRole(ids);
    }

    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    @ApiOperation(value = "修改角色接口", notes = "管理员修改角色信息", httpMethod = "POST", response = ResultJson.class)
    public ResultJson updateRole(@RequestBody @ApiParam(name = "role", value = "角色类类型对象数据",required = false) Role role){
        System.out.println("----修改角色----");
        Login login = (Login) SecurityUtils.getSubject().getPrincipal();
        System.out.println("-=-=-=-=-==-=-=="+login);
//        Login login = new Login();
//        login.setUser_id(1);
        System.out.println(login.getUser_id());
        return roleService.updateRole(role,login);
    }

}
